package com.jinlejia.erp.ui.new_house.apply;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.freedom.yefeng.yfrecyclerview.YfListInterface;
import com.freedom.yefeng.yfrecyclerview.YfListRecyclerView;
import com.freedom.yefeng.yfrecyclerview.YfLoadMoreListener;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.common.WrapContentLinearLayoutManager;
import com.jinlejia.erp.ui.new_house.adapter.ApplyAdapter;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;

/**
 * 申请
 */

public class ApplyListFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, YfLoadMoreListener {

    private View view;
    private YfListRecyclerView recyclerView;
    private ApplyAdapter adapter;

    private static final String STATECODE = "stateCode";
    private String stateCode;
    private int page = 1;
    private SwipeRefreshLayout refreshLayout;
    private boolean mLoadingLock = false;

    public ApplyListFragment() {
        // Required empty public constructor
    }

    public static ApplyListFragment newInstance(String stateCode) {
        ApplyListFragment fragment = new ApplyListFragment();
        Bundle args = new Bundle();
        args.putString(STATECODE, stateCode);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            stateCode = getArguments().getString(STATECODE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_apply_list, container, false);
        initView();
        return view;
    }

    private void initView() {
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.id_swipe_fresh);
        refreshLayout.setColorSchemeResources(R.color.yellow, R.color.light_yellow);

        recyclerView = (YfListRecyclerView) view.findViewById(R.id.id_apply_list);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);
        ArrayList<Map<String, String>> dataList = new ArrayList<>();
        adapter = new ApplyAdapter(dataList);
        recyclerView.setAdapter(adapter);
        getdata(page);
        adapter.setOnItemClickListener(new YfListInterface.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object o) {
                Map<String, String> map = (Map<String, String>) o;
                Intent intent = new Intent(getActivity(), ApplyDetailActivity.class);
                intent.putExtra("dynamicCode", map.get("dynamicCode"));
                startActivity(intent);
            }
        });
        //加载更多
        recyclerView.enableAutoLoadMore(this);
        //刷新
        refreshLayout.setOnRefreshListener(this);
    }

    private void getdata(int p) {
        OkHttpUtils.get()
                .url(String.format(ApiConfig.APPLY_LIST, p))
                .addParams("state", stateCode)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        call.cancel();
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(getActivity(), response, adapter)) {
                            JSONArray data = JSON.parseArray(response.get("data").toString());
                            ArrayList<Map<String, String>> dataList = JSON.parseObject(data.toJSONString(), new TypeReference<ArrayList<Map<String, String>>>() {
                            });
                            mLoadingLock = false;
                            if (page == 1) {
                                adapter.setData(dataList);
                            } else {
                                adapter.addData(dataList);
                            }
                        }
                        if (refreshLayout.isRefreshing()) {
                            refreshLayout.setRefreshing(false);
                        }
                    }
                });
    }

    @Override
    public void onRefresh() {
        page = 1;
        getdata(page);
    }

    @Override
    public void loadMore() {
        if (mLoadingLock) {
            return;
        }
        mLoadingLock = true;
        page++;
        getdata(page);
    }
}
